/**
 * Filename: CommonAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 15:11
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.github.postsanf.yinian.bean.YNImageFloder;
import com.github.postsanf.yinian.utils.LocalViewHolder;

import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;
    private int isPath = 0;

    public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId, int isPath) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
        this.isPath = isPath;
    }

    // 第一个为照相机  所以总数+1
    @Override
    public int getCount() {
        return mDatas.size() + isPath;
    }

    @Override
    public T getItem(int position) {
        if (mDatas.size() == 0) {
            return null;
        } else {
            return mDatas.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LocalViewHolder viewHolder = getViewHolder(position, convertView,
                parent);
        if (getItem(0) instanceof YNImageFloder) {
            convert(viewHolder, getItem(position), false);
            return viewHolder.getConvertView();
        }
        if (position == 0) {
            convert(viewHolder, getItem(position), true);
        } else {
            convert(viewHolder, getItem(position - 1), false);
        }
        return viewHolder.getConvertView();

    }

    public abstract void convert(LocalViewHolder helper, T item, Boolean isFirst);

    private LocalViewHolder getViewHolder(int position, View convertView,
                                          ViewGroup parent) {
        return LocalViewHolder.get(mContext, convertView, parent, mItemLayoutId,
                position);
    }

}
